#define _CRT_SECURE_NO_WARNINGS
#include "Queue.h"

void QueueInit(Queue* pq) {
	assert(pq);
	pq->front = NULL;
	pq->rear = NULL;
	pq->size = 0;
}
void QueueDestroy(Queue* pq) {
	assert(pq);
	QNode* cur = pq->front;
	while (cur) {
		QNode* del = cur;
		cur = cur->next;
		free(del);
	}
	pq->front = pq->rear = NULL;
	pq->size = 0;
	
}
void QueuePush(Queue* pq, QDataType x) {
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL) {
		perror("malloc fail");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->rear == NULL) {
		pq->front = pq->rear = newnode;
	}
	else {
		pq->rear->next=newnode;
		pq->rear = newnode;
	}
	pq->size++;
}
void QueuePop(Queue* pq) {
	assert(pq);
	assert(!QueueEmpty(pq));
	if (pq->front->next == NULL) {
		free(pq->front);
		pq->front = pq->rear = NULL;
	}
	else
	{
		QNode* prve = pq->front;
		pq->front = pq->front->next;
		free(prve);
		
	}
	pq->size--;
	
}
QDataType QueueFront(Queue* pq) {
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->front->data;
}
QDataType QueueBack(Queue* pq) {
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->rear->data;
}
bool QueueEmpty(Queue* pq) {
	assert(pq);
	return pq->front == NULL && pq->rear == NULL;
}
int QueueSize(Queue* pq) {
	assert(pq);
	return pq->size;
}